import hashlib

from django.conf import settings

from jackson.utils import gen_hash

def generate_server_key(ip, port):
    """ Hashes ip and port to generate a key for this server.
    @author: ep
    @param ip: a string representing an IP
    @param port: an integer representing a port number
    @rtype: a string representing a 128-bit hexadecimal hash value
    """
    return gen_hash(ip + ':' + str(port))

def get_key_plus_offset(key, offset):
    """ Adds integer offset to a key in the keyspace (ie the additive group Z/Z_{2^M})
    @author: ep
    @param key: key to add to
    @param offset: an integer (or long integer) to add to key
    @rtype: a key
    @note: This is an egregious hack!
    """
    num = (int(key, 16) + offset) % (2**(settings.M))
    temp = "%032x" % num
    assert len(key) == len(temp)
    return temp
